﻿// <copyright file="AddInComposablePart.cs" company="Anatoly Sereda">
// © 2009 Anatoly Sereda
//
// This source is subject to the Microsoft Public License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Anatoly Sereda</author>
// <email>neonnvr@live.com</email>
// <date>2009-08-04</date>

using System;
using System.AddIn.Hosting;
using System.Collections.Generic;
using System.ComponentModel.Composition.Primitives;
using MafMefInterop.Internal;

namespace MafMefInterop
{
	public class AddInComposablePart : ComposablePart, ICompositionElement
	{
		internal AddInComposablePart(AddInComposablePartDefinition definition)
		{
			this.Definition = definition;
		}

		public override IEnumerable<ExportDefinition> ExportDefinitions
		{
			get { return this.Definition.ExportDefinitions; }
		}

		public override IEnumerable<ImportDefinition> ImportDefinitions
		{
			get { return this.Definition.ImportDefinitions; }
		}

		public string DisplayName
		{
			get { return null; }
		}

		public ICompositionElement Origin
		{
			get { return this.Definition; }
		}

		internal AddInComposablePartDefinition Definition { get; private set; }

		public override object GetExportedValue(ExportDefinition definition)
		{
			AddInActivationPolicy policy;
			if (this.Definition.Catalog.ActivationPolicySelector != null)
			{
				policy = this.Definition.Catalog.ActivationPolicySelector(definition.Metadata);
			}
			else if (this.Definition.Catalog.ActivationPolicy != null)
			{
				policy = this.Definition.Catalog.ActivationPolicy;
			}
			else
			{
				policy = new AddInActivationPolicy(AddInSecurityLevel.Host);
			}

			AddInToken token = this.Definition.AddInToken;
			Type type = this.Definition.ContractType;
			if (policy.Environment != null)
			{
				return token.Activate(type, policy.Environment);
			}
			else
				if (policy.TrustLevel != null)
				{
					if (policy.Process != null)
					{
						return token.Activate(type, policy.Process, policy.TrustLevel.Value);
					}

					if (policy.AppDomainName != null)
					{
						return token.Activate(type, policy.TrustLevel.Value, policy.AppDomainName);
					}
					else
					{
						return token.Activate(type, policy.TrustLevel.Value);
					}
				}
				else if (policy.Permissions != null)
				{
					if (policy.Process != null)
					{
						return token.Activate(type, policy.Process, policy.Permissions);
					}
					else
					{
						return token.Activate(type, policy.Permissions);
					}
				}
				else if (policy.AppDomain != null)
				{
					return token.Activate(type, policy.AppDomain);
				}

			return null;
		}

		public override void SetImport(ImportDefinition definition, IEnumerable<Export> exports)
		{
		}
	}
}
